﻿namespace EMall
{
    public class CodeGenerator
    {
        public static string CreateCode(params int[] numbers)
        {
            if (numbers?.Any() ?? false)
            {
                return string.Empty;
            }
            return string.Join(".", numbers!.Select(number => number.ToString("D5")));
        }
        public static string AppendCode(string parentCode, string childCode)
        {
            if (string.IsNullOrWhiteSpace(parentCode))
            {
                return childCode;
            }
            return $"{parentCode}.{childCode}";
        }
        public static string GetParentCode(string code)
        {
            var splittedCode = code.Split('.');
            if (splittedCode.Length == 1)
            {
                return string.Empty;
            }
            return string.Join(".", splittedCode.Take(splittedCode.Length - 1));
        }
        public static string GetLastCode(string code)
        {
            var splittedCode = code.Split('.');
            return splittedCode.Last();
        }
        public static string CalculateNextCode(string code)
        {
            var parentCode = GetParentCode(code);
            var lastCode = GetLastCode(code);
            var childCode = CreateCode(Convert.ToInt32(lastCode) + 1);
            return AppendCode(parentCode, childCode);
        }
    }
}
